<?php
include '../classes/Authentication.php';
include '../classes/HTML.php';
session_start ();
$auth = new Authentication ();
$auth->checkAuthentication ($_SERVER['PHP_SELF'], 2);
HTML::createHeader('Benutzer', 'user.css');
$db = new DB_For_User();
?>
<body>
		<?php HTML::createLogobar($auth->getUser());?>
	<div class="container">
		<div class="row">
			<div class="col-md-3">
				<h3>Infos</h3>
				<p>Alle Benutzer dieser Inventarisierungs-<br/>software werden hier aufgelistet.</p>
				<p>Als Inventor können Sie folgende Funktionen auf Visitoren anwenden:</p>
				<ul>
					<li>Befördern Visitor -> Inventor</li>
					<li>E-Mail Adresse &auml;ndern</li>
					<li>Passwort &auml;ndern</li>
					<li>Passwort zur&uuml;cksetzen</li>
					<li>Visitor l&ouml;schen</li>
				</ul>
				<p>Suche nach einem User:</p>
				<form method="post" action="users.php">
					<div class="form-group">
						<input type="text" name="email" placeholder="Email" class="form-control" required>
					</div>
					<button type="submit" name="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span> Suche</button>
				</form>
				<br />
				<p>Sie könnnen auch einen neuen User erfassen:</p>
				<form method="post" action="users_create.php">
					<button type="submit" name="create" class="btn btn-default">Erfassen &raquo;</button>
				</form>
			</div>
			<div class="col-md-9">
				<h3>Benutzerrechte &auml;ndern</h3>
				<div class="col-md-6">
				<table class="table table-hover table-condensed">
				<tr>
					<th>ID</th>
					<th>E-Mail</th>
					<th>Rechte</th>
					<th></th>
					<th></th>
				</tr>
				<?php
					if(isset($_POST['submit'])) {
						$email = $_POST['email'];
						$users = $db->getAllUsers($email);
						if($users != null) {
							foreach($users as $user) {
								echo $user->getUserHtmlTable();
								echo '<td><a data-toggle="tooltip" title="Ändern Sie dieses Profil" href=# onclick="document.location.href='; echo "'/users_modify.php?id=" . $user->getUser_id() . "&email=" . $user->getEmail() . "&grant=" . $user->getGrant() . "'"; echo '"><span class="glyphicon glyphicon-wrench"></span></a></td><td><a data-toggle="tooltip" title="Löschen Sie dieses Profil" href=# onclick="if(confirm('; echo "'Sind Sie sicher?'"; echo ')) {'; echo "document.location.href='/users_delete.php?id=" . $user->getUser_id() . "';}"; echo '"><span class="glyphicon glyphicon-remove"></span></a></td></tr>';
							}
						} else {
							echo'<td></td><td>Kein Eintrag vorhanden</td><td></td>';
						}
					} else {
						$users = $db->getAllUsers();
						foreach($users as $user) {
							echo $user->getUserHtmlTable();
							echo '<td><a data-toggle="tooltip" title="Ändern Sie dieses Profil" href=# onclick="document.location.href='; echo "'/users_modify.php?id=" . $user->getUser_id() . "&email=" . $user->getEmail() . "&grant=" . $user->getGrant() . "'"; echo '"><span class="glyphicon glyphicon-wrench"></span></a></td><td><a data-toggle="tooltip" title="Löschen Sie dieses Profil" href=# onclick="if(confirm('; echo "'Sind Sie sicher?'"; echo ')) {'; echo "document.location.href='/users_delete.php?id=" . $user->getUser_id() . "';}"; echo '"><span class="glyphicon glyphicon-remove"></span></a></td></tr>';
						}
					}
				?>
				</table>
				</div>
			</div>
		</div>
		<?php HTML::createFooter();?>
	</div>
	<?php HTML::includeJS();?>
</body>
</html>